package net.daum.hibernate.example;

import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

@Entity
@Table(name = "employee")
public class Employee {

	@Id
	@GeneratedValue
	@Column(name = "employee_id")
	private Long employeeId;

	@Column(name = "firstname")
	private String firstname;

	@Column(name = "lastname")
	private String lastname;

	@Column(name = "birth_date")
	private Date birthDate;

	@Column(name = "cell_phone")
	private String cellphone;

	@ManyToOne
	@JoinColumn(name = "department_id")
	private Department department;

	public Employee() {

	}

	public Employee(String firstname, String lastname, String phone) {
		this.firstname = firstname;
		this.lastname = lastname;
		this.birthDate = new Date(System.currentTimeMillis());
		this.cellphone = phone;
	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public String getCellphone() {
		return cellphone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public static void main(String[] args) {
		//create();
		delete();
	}

	private static void create() {
		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure("net/daum/hibernate/example/hibernate.cfg.xml")
				.buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		Department department = new Department();
		department.setDepartmentName("Sales");

		session.save(department);

		Employee emp1 = new Employee("Nina", "Mayers", "111");
		Employee emp2 = new Employee("Tony", "Almeida", "222");

		emp1.setDepartment(department);
		emp2.setDepartment(department);

		Set<Employee> employeeSet = new HashSet<Employee>();
		employeeSet.add(emp1);
		employeeSet.add(emp2);

		department.setEmployees(employeeSet);
		session.save(department);

		// session.save(emp1);
		// session.save(emp2);

		session.getTransaction().commit();
		session.close();
	}

	public static void list() {

		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure("net/daum/hibernate/example/hibernate.cfg.xml")
				.buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		Department department = (Department) session.load(Department.class, 1L);
		Employee[] employees = department.getEmployees().toArray(
				new Employee[0]);
		for (int i = 0; i < employees.length; i++) {
			System.out
					.println(ToStringBuilder.reflectionToString(employees[i]));
		}

		session.getTransaction().commit();
		session.close();

	}
	
	public static void delete() {

		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure("net/daum/hibernate/example/hibernate.cfg.xml")
				.buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		Department department = (Department) session.load(Department.class, 1L);
		session.delete(department);
		session.getTransaction().commit();
		session.close();

	}
	
	public static void update() {

		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure("net/daum/hibernate/example/hibernate.cfg.xml")
				.buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		Department department = (Department) session.load(Department.class, 1L);
		session.delete(department);
		session.getTransaction().commit();
		session.close();

	}
}